#coding:utf-8
from Tkinter import *
import tkMessageBox
import time
import threading
import json
#import stake_serial as ser
import mySerial as ser
class Application(Frame):
	def __init__(self, master=None):
		Frame.__init__(self, master)
		self.pack()
		self.createWidgets()

	def createWidgets(self):
		#系统信息
		self.LabMsgShow = Label(self, width = 30, font=("微软雅黑", 30, "bold"))
		self.LabMsgShow.grid(row = 0, column = 1, columnspan = 3)

		#卡号
		self.LabID = Label(self, text = '卡    号', font=("微软雅黑", 30, "bold"))
		self.LabID.grid(row = 1, column = 0)

		self.LabIDShow = Label(self, width = 30, font=("微软雅黑", 30, "bold"))
		self.LabIDShow.grid(row = 1, column = 1, columnspan = 3)

		#姓名
		self.LabName = Label(self, text = '姓    名', font=("微软雅黑", 30, "bold"))
		self.LabName.grid(row = 2, column = 0)

		self.LabNameShow = Label(self, width = 30, font=("微软雅黑", 30, "bold"))
		self.LabNameShow.grid(row = 2, column = 1, columnspan = 3)

		#机号
		self.LabMachineID = Label(self, text = '机    号', font=("微软雅黑", 30, "bold"))
		self.LabMachineID.grid(row = 3, column = 0)

		self.LabMachineIDShow = Label(self, width = 30, font=("微软雅黑", 30, "bold"))
		self.LabMachineIDShow.grid(row = 3, column = 3, columnspan = 3)

		#机器密码
		self.LabPassword = Label(self, text = '上机密码', font=("微软雅黑", 30, "bold"))
		self.LabPassword.grid(row = 4, column = 0)

		self.LabPasswordShow = Label(self, width = 30, font=("微软雅黑", 30, "bold"))
		self.LabPasswordShow.grid(row = 4, column = 3, columnspan = 3)

		#上机时间
		self.LabTime = Label(self, text = '上机时间', font=("微软雅黑", 30, "bold"))
		self.LabTime.grid(row = 5, column = 0)

		self.LabTimeShow = Label(self, width = 30, font=("微软雅黑", 30, "bold"))
		self.LabTimeShow.grid(row = 5, column = 3, columnspan = 3)

		#机房号
		self.LabRoomID = Label(self, text = '机 房 号', font=("微软雅黑", 30, "bold"))
		self.LabRoomID.grid(row = 6, column = 0)

		self.LabRoomIDShow = Label(self, width = 30, font=("微软雅黑", 30, "bold"))
		self.LabRoomIDShow.grid(row = 6, column = 3, columnspan = 3)

		#有效时间
		self.LabEffective = Label(self, text = '有效时间', font=("微软雅黑", 30, "bold"))
		self.LabEffective.grid(row = 7, column = 0)

		self.LabEffectiveShow = Label(self, width = 30, font=("微软雅黑", 30, "bold"))
		self.LabEffectiveShow.grid(row = 7, column = 1, columnspan = 3)

		#开始 结束 按钮
		self.btnStart = Button(self, text = '开 始', command = self.start, font=("微软雅黑", 16, "bold"))
		self.btnStart.grid(row = 8, column = 1)

		self.btnEnd = Button(self, text = '停 止', command = self.end, state = DISABLED, font=("微软雅黑", 16, "bold"))
		self.btnEnd.grid(row = 8, column = 3)
		
		#刷卡按钮
		self.btnCard = Button(self, text = '刷卡', command = self.do, state = DISABLED)
		self.btnCard.grid(row = 9, column = 0, columnspan = 4)

	def start(self):
		#更改按钮状态
		self.btnEnd['state'] = ACTIVE
		self.btnCard['state'] = ACTIVE
		self.btnStart['state'] = DISABLED


	def end(self):
		#修改按钮状态
		self.btnEnd['state'] = DISABLED
		self.btnCard['state'] = DISABLED
		self.btnStart['state'] = ACTIVE

	def do(self):
		id = self.getID()
		ser.write(id)
		studentInfo = ser.read()
		if studentInfo is None:
			return
		studentInfo = json.loads(studentInfo)
		studentInfo = json.loads(studentInfo)
		if studentInfo['isValid'] == True:
			self.LabMsgShow['text'] = '刷卡成功！'
			self.LabIDShow['text'] = studentInfo['StuInfo']['StuID']
			self.LabEffectiveShow['text'] = studentInfo['StuInfo']['Date']
			self.LabMachineIDShow['text'] = studentInfo['Computer']['ComputerID']
			self.LabNameShow['text'] = studentInfo['StuInfo']['Name']
			self.LabRoomIDShow['text'] = studentInfo['Computer']['RoomID']
			self.LabPasswordShow['text'] = studentInfo['Computer']['Password']
			self.LabTimeShow['text'] = studentInfo['Time']
		else:
			self.LabIDShow['text'] = ""
			self.LabEffectiveShow['text'] = ""
			self.LabMachineIDShow['text'] = ""
			self.LabNameShow['text'] = ""
			self.LabRoomIDShow['text'] = ""
			self.LabPasswordShow['text'] = ""
			self.LabTimeShow['text'] = ""
			if studentInfo['errormsg'] == "Invalid ID!":
				self.LabMsgShow['text'] = '卡号不正确！'
			elif studentInfo['errormsg'] == "Not make reservation!":
				self.LabMsgShow['text'] = '未找到预约信息！'
			elif studentInfo['errormsg'] == "Reservation has been done!":
				self.LabMsgShow['text'] = '预约已完成'
			elif studentInfo['errormsg'] == "Reservation not done>2!":
				self.LabMsgShow['text'] = '您已进入黑名单，请联系管理员！'
			elif studentInfo['errormsg'] == "No computer available!":
				self.LabMsgShow['text'] = '没有可用机器！'
			elif studentInfo['errormsg'] == 'OK':
				self.LabMsgShow['text'] = '下机成功！'

	def getID(self):
		return "913106840331"

if __name__ == '__main__':
	app = Application()
	# 设置窗口标题:
	app.master.title('Tkinter')
	# 主消息循环:
	app.mainloop()